package com.ict.ms.trackPlayBack.facade.redis.impl;

import com.ict.ms.trackPlayBack.facade.common.SplitUtils2;
import com.ict.ms.trackPlayBack.facade.redis.dao.ITrackFuseDao;
import com.ict.ms.trackPlayBack.facade.redis.pojo.Fuse;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * Created by chenlong on 2016/11/14.
 */
public class TrackFuseDaoImpl implements ITrackFuseDao {
    @Resource
    private RedisTemplate<Serializable, Serializable> redisTemplate;

    /**
     *
     * @param mdv
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Fuse> getFuse_original(final String mdv, final int startTime, final int endTime) {
        return redisTemplate.execute(new RedisCallback<List<Fuse>>() {
            @Override
            public List<Fuse> doInRedis(RedisConnection connection)
                    throws DataAccessException {
                byte[] key = redisTemplate.getStringSerializer().serialize(mdv);
                if (connection.exists(key)) {
                    List<Fuse> trackFuseList=new ArrayList<Fuse>();

                    Set<RedisZSetCommands.Tuple> value = connection.zRangeByScoreWithScores(key,startTime,endTime);

                    Iterator<RedisZSetCommands.Tuple> it=value.iterator();
                    while (it.hasNext()){
//                        将取出来的byte[]类型转换成String类型
                        String v=new String(it.next().getValue());
                        System.out.println((v));

                        Fuse trackFuse=getFuse(v);
                        trackFuseList.add(trackFuse);
                    }
                    System.out.println(value);
                    return trackFuseList;
                }
//                todo 限制条数
                return null;
            }
        });
    }

    @Override
    public List<Fuse> getFuse_original_Track(final String mdv,final int limit) {
        return redisTemplate.execute(new RedisCallback<List<Fuse>>() {
            @Override
            public List<Fuse> doInRedis(RedisConnection connection)
                    throws DataAccessException {

                byte[] key = redisTemplate.getStringSerializer().serialize(mdv);
                if (connection.exists(key)) {
                    List<Fuse> trackFuseList=new ArrayList<Fuse>();

                    Set<RedisZSetCommands.Tuple> value;
                    value = connection.zRangeWithScores(key,limit,-1);

                    Iterator<RedisZSetCommands.Tuple> it=value.iterator();
                    while (it.hasNext()){
//                        将取出来的byte[]类型转换成String类型
                        String v=new String(it.next().getValue());
                        System.out.println((v));

                        Fuse trackFuse=getFuse(v);
                        trackFuseList.add(trackFuse);
                    }
                    System.out.println(value);
                    return trackFuseList;
                }

////                todo 限制条数
                return null;
            }
        });
    }

    //    14787679021~1~72932609~16940444~290~29~23~412345678
//    将取出来的如上面格式的redis值变成对象方便放入List中
    private Fuse getFuse(String str){
        Fuse fuse=new Fuse();

        try{
            String[] strings= SplitUtils2.MDVNumSplit_StrArray(str);
            fuse.setTi(Integer.parseInt(strings[0]));
            fuse.setLo(Integer.parseInt(strings[1]));
            fuse.setLa(Integer.parseInt(strings[2]));
            fuse.setCo(Integer.parseInt(strings[3]));
            fuse.setHe(Integer.parseInt(strings[4]));
            fuse.setSp(Integer.parseInt(strings[5]));

            return fuse;
        }catch (Exception e){
            System.out.println("取出的redis值转换成对象失败"+e);
            return null;
        }
    }
}